#ifndef _INCLUDE_INTERRUPT_H_
#define _INCLUDE_INTERRUPT_H_

#include "global.h"
#include "stdint.h"

#define PIC_M_CTRL 0x20 // 这里用的可编程中断控制器是8259A,主片的控制端口是0x20
#define PIC_M_DATA 0x21 // 主片的数据端口是0x21
#define PIC_S_CTRL 0xa0 // 从片的控制端口是0xa0
#define PIC_S_DATA 0xa1 // 从片的数据端口是0xa1

#define IDT_DESC_CNT 0x81

/// @brief interrupt function handler
typedef void* intr_handler;

/// @brief interrupt gate descriptor struction
struct gate_desc
{
  uint16_t func_offset_low_word;
  uint16_t selector;
  uint8_t  dcount;
  uint8_t  attribute;
  uint16_t func_offset_high_word;
};

/// @brief interrupt status
/// @details 0 means off; 1 means on
enum intr_status
{
  INTR_OFF,
  INTR_ON
};

/// @brief init everything about idt
void idt_init();

/// @brief init interrupt descriptor table
void idt_desc_init();

/// @brief get the interrupt status
/// @return current status of interrupt
enum intr_status intr_get_status();

/// @brief set the interrupt status
/// @return current status of interrupt
enum intr_status intr_set_status(enum intr_status status);

/// @brief enable the interrupt
/// @return old status of interrupt
enum intr_status intr_enable();

/// @brief disable the interrupt
/// @return old status of interrupt
enum intr_status intr_disable();

/// @brief Register interrupt handler
/// @param vector_no Interrupt vector number, specifying a particular interrupt
/// @param function Interrupt handler function to be executed when the specified
/// interrupt occurs
void register_handler(uint8_t vector_no, intr_handler function);

#endif
